#ifndef _IO_C
#define _IO_C

#define IN_BUFFER_SIZE     8
#define OUT_BUFFER_SIZE    8

char inBuffer[IN_BUFFER_SIZE];
//char outBuffer[OUT_BUFFER_SIZE];

char inStart, inEnd, outStart, outEnd;
//char inStart, inEnd, inSize, outStart, outEnd, outSize;

void io_config( void )
{
#if FOSC == 4000000
   BRG16 = 0;
   SYNC = 0;
   BRGH = 1;
   SPBRGH = 0;
   SPBRG = 25;
#elif FOSC == 16000000
   BRG16 = 0;
   SYNC = 0;
   BRGH = 0;
   SPBRGH = 0;
   SPBRG = 25;
#else
#error There is no baud rate setting for the chosen Fosc speed.
#endif

   SPEN = 1;   // Enable serial port
   TXEN = 1;   // Enable transmit

   RCIP = 0;   // Rx is low priority
   TXIP = 0;   // Tx is low priority

   RCIE = 0;   // Rx buffer is empty
   TXIE = 0;   // Tx buffer is empty

   inStart = 0;
   inEnd = 0;
   outStart = 0;
   outEnd = 0;
}

char putchar( char data )
{
   // Check to see if buffer is full.
   // Indicated by start=end AND interrupt already enabled
   if( (inStart==inEnd) && (TXIE==1) )
      return( 1 );
   
   inBuffer[inStart] = data;
   inStart++;
   if( inStart >= IN_BUFFER_SIZE )
      inStart = 0;

   TXIE = 1;
   return( 0 );
}

void isr_putchar( void )
{
   if( !TXIE )
      return;

   TXREG = inBuffer[inEnd];
   inEnd++;
   if( inEnd >= IN_BUFFER_SIZE )
      inEnd = 0;

   // disable interrupt if buffer is empty.
   if( inEnd == inStart )
      TXIE = 0;
}

void isr_getchar( void )
{
}

#endif


